#ifndef _FRAME_H_
#define _FRAME_H_

#include "buffer.h"

#ifdef __cplusplus
extern "C"{
#endif

#define FRAME_DEF_SIZE 256
#define FRAME_SYNC		0xA5
typedef struct frame frame_t;

struct frame {
	struct frame *next;
	uint8_t sync;
	uint8_t id;
	uint32_t len;
	uint32_t size;
	union {
		uint8_t data[FRAME_DEF_SIZE];
		buffer_t buf;	
	};
};

int frame_init(frame_t *self, frame_t *next, uint8_t id, uint32_t size);
void frame_term(frame_t *self);
void frame_reset(frame_t *self);

static inline frame_t *frame_new(frame_t *next, uint8_t id, uint32_t size)
{
	frame_t *self = (frame_t *)MALLOC(sizeof(frame_t));
	if (self) {
		if (frame_init(self, next, id, size) < 0)
			safe_free(self);
	}

	return self;
}

static inline void frame_destroy(frame_t **p_self)
{
	if (p_self && *p_self) {
		frame_t *self = *p_self;
		frame_term(self);
		safe_free(*p_self);
	}
}

int frame_add(frame_t *self, void *data, size_t len);

#ifdef __cplusplus
}
#endif

#endif
